/** \file TeamManager.cpp Contains the Implementation of the TeamManager Class.
 *  \author Adrian Schweizer
 *  \created  $Sa 18 Aug 03:48:01 pm CEST 2007 schwadri@SchwadriComp.local$
 *  \modified $Sa 06 Okt 06:22:38 pm CEST 2007 schwadri@SchwadriLaptop$
 */

#include "TeamManager.hpp"

using namespace game;

TeamManager::TeamManager()
:   m_spectators(this,-1)
{ }

TeamManager::~TeamManager()
{ }

void TeamManager::RemoveTeam(char tid)
{
    if(tid<0 || tid>=m_teams.size())
        return;

    team_iterator titer =   m_teams.begin()+tid;
    titer = m_teams.erase(titer);

    //shift team and player ids
    ShiftTeamAndPlayerIds(titer);
}

void TeamManager::ShiftTeamAndPlayerIds(team_iterator titer)
{
    char tidx = titer-m_teams.begin();
    for(;titer!=m_teams.end();++titer,++tidx)
        titer->TeamId(tidx);
}

void TeamManager::CreateNewTeam()
{
    if(m_teams.size()>=LIMIT_NUMBER_OF_TEAMS)
        return;
    char tid =   m_teams.size();
    m_teams.push_back(Team(this,tid));
    m_teams.back().OpenPlayerSlots(1);
}

void TeamManager::RemovePlayer(Player::Id pid)
{
    if(!pid.Valid())
        return;
    m_teams[pid.team].Remove(pid);
}
